package com.zhangtai.modules.controller.member;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zhangtai.modules.controller.aouthLogin.MemberLoginController;
import com.zhangtai.modules.dao.ContractUrlDao;
import com.zhangtai.modules.dao.ImgUrlDao;
import com.zhangtai.modules.dao.RelativeRecordsDao;
import com.zhangtai.modules.dto.EmployeeInfoDto;
import com.zhangtai.modules.dto.JobDto;
import com.zhangtai.modules.dto.JobLevelDto;
import com.zhangtai.modules.dto.OldEmployeeDto;
import com.zhangtai.modules.entity.common.ContractUrlEntity;
import com.zhangtai.modules.entity.common.ImgUrlEntity;
import com.zhangtai.modules.entity.common.OrgEntity;
import com.zhangtai.modules.entity.employee.*;
import com.zhangtai.modules.entity.salary.OrgSystemEntity;
import com.zhangtai.modules.service.baseFromMssql.BaseMssqlServiceImpl;
import com.zhangtai.modules.service.common.ImgUrlService;
import com.zhangtai.modules.service.employee.*;
import com.zhangtai.modules.vo.*;
import com.zhangtai.utils.BeanUtilsMabach;
import com.zhangtai.utils.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author: ming
 * @Date: 2020/6/15 0015 下午 1:36
 */
@Api(tags="员工自助端接口")
@RestController
@RequestMapping("selfHelp")
@Slf4j
public class SelfHelpMemberController {
    @Autowired
    private EmployeeInfoService employeeInfoService;

    @Autowired
    private EmployeeInorgnameService employeeInorgnameService;
    @Autowired
    private EmployessSocietyexperienceService employessSocietyexperienceService;
    @Autowired
    private FamilyMemberService familyMemberService;
    @Autowired
    private ImgUrlService imgUrlService;
    @Autowired
    private ImgUrlDao imgUrlDao;
    @Autowired
    private EmployeeBpmService employeeBpmService;


    @Autowired
    private EmployeeEducationService employeeEducationService;
    @Autowired
    BaseMssqlServiceImpl jobService;

    @Autowired
    private ContractUrlDao contractUrlDao;
    @Autowired
    private EmployerCompanyService employerCompanyService;
    @Autowired
    private RelativeRecordsDao relativeRecordsDao;

    @Autowired
    private MemberLoginController memberLoginController;






    @GetMapping("/getEmployeeById")
    @ApiOperation(value="根据员工id获取详情(从现有库中查询)")
    // @SysLog(value ="测试[{{id}}]备份",operationType = OperationType.SELECT)
    public R<EmployeeInfoDto> findEmployeeByOid(HttpServletRequest request) throws Exception {
        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (ObjectUtils.isEmpty(data)){
            return R.error("请先登录");
        }
        Long oid = data.getLong("id");
        EmployeeInfoEntity employeeInfoEntity = employeeInfoService.getByIdE(oid);
        if (ObjectUtils.isEmpty(employeeInfoEntity)){
            return R.ok();
        }


        List<EmployeeEducationEntity> educationEntities = employeeEducationService.findEmployeeEducationListByOid(oid);
        List<EmployeeInorgnameEntity> inorgnameEntityList = employeeInorgnameService.getByEmpId(oid);
//        log.info("政治面貌:{}",inorgnameEntityList);
        List<EmployessSocietyexperienceEntity> societyexperienceEntities = employessSocietyexperienceService.getByEmpId(oid);
        //查询员工合同记录
        List<ContractUrlEntity> contractList= contractUrlDao.selectList(new QueryWrapper<ContractUrlEntity>().eq("is_delete",0).eq("id_card",employeeInfoEntity.getIdcard()));
        List<FamilyMemberEntity> familyMemberEntities = familyMemberService.getListByEmpId(oid);
        //        亲属报备
        List<RelativeRecordsEntity> relativeRecordsEntities = relativeRecordsDao.selectList(new QueryWrapper<RelativeRecordsEntity>().eq("oid", oid).eq("is_delete", 0));

        EmployeeInfoDto employeeInfoDto = BeanUtilsMabach.doToDto(employeeInfoEntity, EmployeeInfoDto.class);
        employeeInfoDto.setEmployeeEducation(educationEntities);
        employeeInfoDto.setEmployeeInorgname(inorgnameEntityList);
        employeeInfoDto.setFamilyMember(familyMemberEntities);
        employeeInfoDto.setEmployeeSociety(societyexperienceEntities);
        employeeInfoDto.setContractList(contractList);
        employeeInfoDto.setRelativeRecordsEntities(relativeRecordsEntities);


        getImg(oid, employeeInfoDto); //封装图片


        /**
         * 根据各种id查询名称
         */
        Long unitId = employeeInfoEntity.getUnitId();
        Long orgId = employeeInfoEntity.getOrgId();
        Long jobId = employeeInfoEntity.getJobId();

        Long jobSystemId = employeeInfoEntity.getJobSystemId();
        Long lineId = employeeInfoEntity.getLineId();



        Long jobLevelId = employeeInfoEntity.getJobLevelId();
        JobDto jobDto = jobService.findJobById(jobId);



        JobLevelDto jobLevelById = jobService.findJobLevelById(jobLevelId);
        log.info("JobLevelDto:{}",jobLevelById);
        if (!ObjectUtils.isEmpty(jobLevelById)){
            employeeInfoDto.setJobLevel(jobLevelById.getName());
        }




        OrgSystemEntity systemById = jobService.findSystemById(jobSystemId);
//        log.info("OrgSystemEntity:{}",jobLevelById);
        if (!ObjectUtils.isEmpty(systemById)){
            employeeInfoDto.setJobSystem(systemById.getName());
        }
        OrgSystemEntity lineById = jobService.findLineById(lineId);
//        log.info("OrgSystemEntity:{}",jobLevelById);
        if (!ObjectUtils.isEmpty(lineById)){
            employeeInfoDto.setLine(lineById.getName());
        }
        OrgEntity org = jobService.findOrgById(orgId);
        if (!ObjectUtils.isEmpty(org)){
            employeeInfoDto.setOrgname(org.getName());
        }

        OrgEntity unit = jobService.findOrgById(unitId);
        if (!ObjectUtils.isEmpty(unit)){
            employeeInfoDto.setUnitname(unit.getName());
        }

        JobDto jobByJobId = jobService.findJobById(jobId);
//        log.info("JobDto:{}",jobLevelById);
        if (!ObjectUtils.isEmpty(jobByJobId)){
            employeeInfoDto.setJobname(jobByJobId.getCname());
        }
        if (!ObjectUtils.isEmpty(jobDto)){
            employeeInfoDto.setJobname(jobDto.getCname());
        }
        Long employerCompanyId = employeeInfoEntity.getEmployerCompanyId();
        EmployerCompanyEntity byIdE = employerCompanyService.getByIdE(employerCompanyId);
        if (!ObjectUtils.isEmpty(byIdE)){
            //        封装甲方单位
            employeeInfoDto.setEmployerCompany(byIdE.getName());
        }


//        封装复职员工信息
        Integer isReinstate = employeeInfoEntity.getIsReinstate(); //0:新员工，1：复职员工，2：旧员工
        Integer i=1;
        if (i.equals(isReinstate)){
            EmployeeBpmEntity employeeBpmEntity = employeeBpmService.getByIdE(oid);
            if (!ObjectUtils.isEmpty(employeeBpmEntity)){
                OldEmployeeDto oldEmployeeDto = BeanUtilsMabach.doToDto(employeeBpmEntity, OldEmployeeDto.class);
                employeeInfoDto.setOldEmployeeDto(oldEmployeeDto);
            }
        }



        return R.ok(employeeInfoDto);
    }


    @GetMapping("/getRelativeRecords")
    @ApiOperation(value="根据oid获取员工亲属报备信息")
    public R<List<RelativeRecordsEntity>> getRelativeRecords(HttpServletRequest request)throws  Exception{

        JSONObject data = memberLoginController.getUserInfo(request).getData();
        if (org.apache.commons.lang3.ObjectUtils.isEmpty(data)){
            return R.error("请先登录");
        }
        Long oid = data.getLong("id");

        List<RelativeRecordsEntity> relativeRecordsEntities = relativeRecordsDao.selectList(new QueryWrapper<RelativeRecordsEntity>().
                eq("oid", oid).eq("is_delete", 0));



        return R.ok(relativeRecordsEntities);
    }







    private Long saveImg(@RequestBody @Valid MemberEmpBpmUpdateVo memberEmpBpmUpdateVo) {
        Long id = memberEmpBpmUpdateVo.getOid();
//修改流水
        List<String> oldSalaryRecordUrl = memberEmpBpmUpdateVo.getOldSalaryRecordUrl();
        imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",1));
        if (!CollectionUtils.isEmpty(oldSalaryRecordUrl)){

            for (String s : oldSalaryRecordUrl) {
                ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
                imgUrlEntity.setName("原公司工资流水");
                imgUrlEntity.setOid(id);
                imgUrlEntity.setType(1);
                imgUrlEntity.setCreateTime(new Date());
                imgUrlEntity.setUrl(s);
                imgUrlService.saveE(imgUrlEntity);

            }
        }
//        照片
        String photoUrl = memberEmpBpmUpdateVo.getPhotoUrl();
        if (!StringUtils.isEmpty(photoUrl)){

            imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",0));
            ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
            imgUrlEntity.setType(0);
            imgUrlEntity.setOid(id);
            imgUrlEntity.setName("1寸照片");
            imgUrlEntity.setUrl(photoUrl);
            imgUrlEntity.setCreateTime(new Date());

            imgUrlService.saveE(imgUrlEntity);
        }

        //        毕业证
        String graduationUrl = memberEmpBpmUpdateVo.getGraduationUrl();
        if (!StringUtils.isEmpty(graduationUrl)){
            imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",2));
            ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
            imgUrlEntity.setType(2); //0:流水，1：头像，2：毕业证
            imgUrlEntity.setOid(id);
            imgUrlEntity.setName("毕业证");
            imgUrlEntity.setUrl(graduationUrl);
            imgUrlEntity.setCreateTime(new Date());
            imgUrlService.saveE(imgUrlEntity);
        }
//        学位证
        String degreeUrl = memberEmpBpmUpdateVo.getDegreeUrl();
        imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",3));
        if (!StringUtils.isEmpty(degreeUrl)){

            ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
            imgUrlEntity.setType(3); //0:流水，1：头像，2：毕业证,3:学位证
            imgUrlEntity.setOid(id);
            imgUrlEntity.setName("学位证");
            imgUrlEntity.setUrl(degreeUrl);
            imgUrlEntity.setCreateTime(new Date());
            imgUrlService.saveE(imgUrlEntity);
        }
//        离职证明
        String dimissionUrl = memberEmpBpmUpdateVo.getDimissionUrl();
        imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",4));
        if (!StringUtils.isEmpty(dimissionUrl)){
            ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
            imgUrlEntity.setType(4); //0:流水，1：头像，2：毕业证,3:学位证，4：离职证明
            imgUrlEntity.setOid(id);
            imgUrlEntity.setName("离职证明");
            imgUrlEntity.setUrl(degreeUrl);
            imgUrlEntity.setCreateTime(new Date());
            imgUrlService.saveE(imgUrlEntity);
        }

//        体检报告
        String healthUrl = memberEmpBpmUpdateVo.getHealthUrl();
        imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",5));
        if (!StringUtils.isEmpty(healthUrl)){
            ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
            imgUrlEntity.setType(5); //0:流水，1：头像，2：毕业证,3:学位证，4：离职证明,5：体检报告
            imgUrlEntity.setOid(id);
            imgUrlEntity.setName("体检报告");
            imgUrlEntity.setUrl(degreeUrl);
            imgUrlEntity.setCreateTime(new Date());
            imgUrlService.saveE(imgUrlEntity);
        }
//        身份证
        List<String> idcardImgs = memberEmpBpmUpdateVo.getIdcardImgs();
        if (!CollectionUtils.isEmpty(idcardImgs)){
            imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",6));
            for (String idcardImg : idcardImgs) {
                ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
                imgUrlEntity.setType(6); //0:流水，1：头像，2：毕业证,3:学位证，4：离职证明,5：体检报告,6:身份证正反面
                imgUrlEntity.setOid(id);
                imgUrlEntity.setName("身份证");
                imgUrlEntity.setUrl(idcardImg);
                imgUrlEntity.setCreateTime(new Date());
                imgUrlService.saveE(imgUrlEntity);
            }

        }
        List<String> credential = memberEmpBpmUpdateVo.getCredential(); //证书
        imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",10));
        if (!CollectionUtils.isEmpty(credential)){
            for (String s : credential) {
                ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
                imgUrlEntity.setType(10); //0:流水，1：头像
                imgUrlEntity.setOid(id);
                imgUrlEntity.setName("职称证书");
                imgUrlEntity.setUrl(s);
                imgUrlEntity.setCreateTime(new Date());
                imgUrlService.saveE(imgUrlEntity);
            }
        }
        List<String> attachments = memberEmpBpmUpdateVo.getAttachments();
        imgUrlDao.delete(new QueryWrapper<ImgUrlEntity>().eq("oid",id).eq("type",11));
        if (!CollectionUtils.isEmpty(attachments)){
            for (String attachment : attachments) {
                ImgUrlEntity imgUrlEntity = new ImgUrlEntity();
                imgUrlEntity.setType(11); //0:流水，1：头像
                imgUrlEntity.setOid(id);
                imgUrlEntity.setName("其他附件");
                imgUrlEntity.setUrl(attachment);
                imgUrlEntity.setCreateTime(new Date());
                imgUrlService.saveE(imgUrlEntity);
            }
        }

        return id;
    }



    private void getImg(Long oid, EmployeeInfoDto employeeInfoDto) {
        //        如果薪资流水不为空，则封装进去
        List<ImgUrlEntity> imgUrlEntities = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 1));
        if (!CollectionUtils.isEmpty(imgUrlEntities)){
            ArrayList<String> recordUrls = new ArrayList<>();
            for (ImgUrlEntity imgUrlEntity : imgUrlEntities) {
                recordUrls.add(imgUrlEntity.getUrl());
            }
            employeeInfoDto.setOldSalaryRecordUrl(recordUrls);
        }
//        封装头像
        ImgUrlEntity imgUrlEntity = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 0));
        if (!ObjectUtils.isEmpty(imgUrlEntity)){
            employeeInfoDto.setPhotoUrl(imgUrlEntity.getUrl());
        }

        //0:流水，1：头像，2：毕业证,3:学位证，4：离职证明,5：体检报告
        ImgUrlEntity imgUrlEntity2 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 2));
        if (!ObjectUtils.isEmpty(imgUrlEntity2)){
            employeeInfoDto.setGraduationUrl(imgUrlEntity2.getUrl());
        }
//        学位证
        ImgUrlEntity imgUrlEntity3 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 3));
        if (!ObjectUtils.isEmpty(imgUrlEntity3)){
            employeeInfoDto.setDegreeUrl(imgUrlEntity3.getUrl());
        }
//        离职证明
        ImgUrlEntity imgUrlEntity4 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 4));
        if (!ObjectUtils.isEmpty(imgUrlEntity4)){
            employeeInfoDto.setDimissionUrl(imgUrlEntity4.getUrl());
        }
//        体检报告
        ImgUrlEntity imgUrlEntity5 = imgUrlDao.selectOne(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 5));
        if (!ObjectUtils.isEmpty(imgUrlEntity5)){
            employeeInfoDto.setHealthUrl(imgUrlEntity5.getUrl());
        }

        //        身份证
        List<ImgUrlEntity> imgUrlEntityList = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 6));
        ArrayList<String> strings = new ArrayList<>();
        if (!CollectionUtils.isEmpty(imgUrlEntityList)){
            for (ImgUrlEntity urlEntity : imgUrlEntityList) {
                strings.add(urlEntity.getUrl());
            }
            employeeInfoDto.setIdcardImgs(strings);
        }
//职称
        List<ImgUrlEntity> imgUrlEntityList3 = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 10));
        ArrayList<String> strings1 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(imgUrlEntityList3)){
            for (ImgUrlEntity urlEntity : imgUrlEntityList3) {
                strings1.add(urlEntity.getUrl());
            }
            employeeInfoDto.setCredential(strings1);
        }

//        其他附件

        List<ImgUrlEntity> imgUrlEntityList4 = imgUrlDao.selectList(new QueryWrapper<ImgUrlEntity>().eq("oid", oid).eq("type", 11));
        ArrayList<String> strings2 = new ArrayList<>();
        if (!CollectionUtils.isEmpty(imgUrlEntityList4)){
            for (ImgUrlEntity urlEntity : imgUrlEntityList4) {
                strings2.add(urlEntity.getUrl());
            }
            employeeInfoDto.setAttachments(strings2);
        }

    }







}
